package wh.coach.utils;

import java.io.IOException;

import lombok.extern.slf4j.Slf4j;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.ResponseBody;

/**
 * @author xhw
 */
@Slf4j
public class HttpReadUtils {

    public static void readUrl(final String url, final UrlStreamConsumer consumer) {
        //创建一个http对象client
        OkHttpClient client = new OkHttpClient();
        //构建一个事例，包含之前下载结束位置的信息
        Request request = new Request.Builder()
                .url(url)
                .method("GET", null)
                .build();

        try (Response response = client.newCall(request).execute()) {
            if (response.isSuccessful()) {
                String dataType = response.header("Content-Type");
                ResponseBody responseBody = response.body();

                if (responseBody != null) {
                    consumer.consume(dataType, responseBody.byteStream());
                }
            }
        } catch (IOException e) {
            log.error(
                    "occurs io error while call url [{}],cause by [{}]",
                    url, e.getMessage()
            );

            throw new RuntimeException(e.getMessage(), e);
        }
    }

    public static BytesWrapper readUrlAsBytes(final String url) {

        //创建一个http对象client
        OkHttpClient client = new OkHttpClient();
        //构建一个事例，包含之前下载结束位置的信息
        Request request = new Request.Builder()
                .url(url)
                .method("GET", null)
                .build();

        try (Response response = client.newCall(request).execute()) {
            if (response.isSuccessful()) {
                String dataType = response.header("Content-Type");
                ResponseBody responseBody = response.body();

                if (responseBody != null) {
                    //用于存储从响应体中读取的字节数据
                    return new BytesWrapper(true, dataType, responseBody.bytes());
                } else {
                    return new BytesWrapper(true, dataType, new byte[0]);
                }
            } else {
                String dataType = response.header("Content-Type");
                ResponseBody responseBody = response.body();

                if (responseBody != null) {
                    final String bodyStr = responseBody.string();
                    log.error(
                            "response error while call url [{}],status code :[{}],reason:[{}]",
                            url, response.code(), bodyStr
                    );

                    return new BytesWrapper(false, dataType, bodyStr.getBytes());
                } else {
                    log.error(
                            "response error while call url [{}],status code :[{}]",
                            url, response.code()
                    );

                    return new BytesWrapper(false, dataType, new byte[0]);
                }
            }
        } catch (IOException e) {
            log.error(
                    "occurs io error while call url [{}],cause by [{}]",
                    url, e.getMessage()
            );

            throw new RuntimeException(e.getMessage(), e);
        }
    }

}
